<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class UserController extends BaseController{
	//用户登录验证
	public function login_json(){
		$username=$_POST['username'];
		$passwd=$_POST['passwd'];
		$data=$this->checkLogin($username,$passwd);				
		$this->Response($data,'json');
	}

	public function checkLogin($username,$passwd){
		$map['name']=$username;
		$map['pass']=md5($passwd);
		$res=M('Customer')->where($map)->find();
		if($res){
			session('username',$res['name']);
			session('uid',$res['id']);
			$data=array(
				'username'=>$username,
				'password'=>$passwd,
				'msg'=>"登陆成功",
				'error_no'=>10001,
				'status'=>1,
			);
		}else{
			$data=array(
				'msg'=>"账户或密码错误",
				'error_no'=>20001,
				'status'=>0,
			);
		}		
		return $data;	
		

	}
	public function logout_post(){		
			$_SESSION['customer']=null;
			$data=array(			//
				'error_no'=>10002,
				'msg'=>'注销成功',
				'custemer'=>null,
				'status'=>1,
			);
		
		$this->Response($data,'json');
	}
	public function register_post_json(){
		$data['name']=$_GET['name'];
		$data['pass']=md5($_GET['pass']);
		$data['sign']=$_GET['sign'];
		$data['face']=$_GET['face'];		
		$res=M('Customer')->add($data);
			if($res){
				$data=array(
					'error_no'=>10003,
					'msg'=>'注册成功',
					'status'=>1,
				);		
			}else{
				$data=array(
					'error_no'=>20003,
					'msg'=>'注册失败',
					'status'=>0,
				);	
			}
		$this->Response($data,'json');

	}
	public function update_put_json(){	
		$map['id']=$_SESSION['customer']['id'];
		$res=M('Customer')->where($map)->save($_GET);
			if($res!==false){
				$data=array(
					'error_no'=>10005,
					'msg'=>'更新成功',
					'status'=>1,
				);		
			}else{
				$data=array(
					'error_no'=>20005,
					'msg'=>'更新失败',
					'status'=>0,
				);	
			}
			
		$this->Response($data,'json');
	}
	public function info_get_json(){			
		$res=M('Customer')->find($_SESSION['customer']['id']);
		if($res){
			$data=array(
			 'error_no'=>10006,
			 'msg'=>'个人信息获取成功',
			 'customer'=>$res,
			 'status'=>1,
			);	
		}else{
			$data=array(
				'error_no'=>10006,
				'msg'=>'个人信息获取失败',
				'customer'=>$res,
				'status'=>0,
			);	
		}
		$this->Response($data,'json');


	}
}